package domain.model;

import java.io.Serializable;
import java.io.UnsupportedEncodingException;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.Date;

import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;

import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.Text;
import com.google.appengine.repackaged.com.google.common.util.Base64;


import domain.service.Utils;

@PersistenceCapable
public class Whois implements Serializable{
	@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
	@PrimaryKey
	private Key key;
	@Persistent
	private String name;
	@Persistent
	private Text whois;
	@Persistent
	private ArrayList<String> email = new ArrayList<String>();
	@Persistent
	private String createdDate;
	@Persistent
	private String expirationDate;
	@Persistent
	private Date lastUpdateDate;
	@Persistent
	private String user;
	@Persistent
	private String aliasUser;
	@Persistent
	private ArrayList<String> server = new ArrayList<String>();
	public String getAliasUser() {
		return aliasUser;
	}
	public void setAliasUser(String aliasUser) {
		this.aliasUser = aliasUser;
	}
	public void addServer(String server)
	{
		if(!this.server.contains(server))
			this.server.add(server);
	}
	public void addEmail(String email)
	{
		if(!this.email.contains(email))
			this.email.add(email);
	}
	public Key getKey() {
		return key;
	}
	public void setKey(Key key) {
		this.key = key;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public Text getWhois() {
		return whois;
	}
	public void setWhois(Text whois) {
		this.whois = whois;
	}
	public ArrayList<String> getEmail() {
		return email;
	}
	public void setEmail(ArrayList<String> email) {
		this.email = email;
	}
	public String getCreatedDate() {
		return createdDate;
	}
	public void setCreatedDate(String createdDate) {
		this.createdDate = createdDate;
	}
	public String getExpirationDate() {
		return expirationDate;
	}
	public void setExpirationDate(String expirationDate) {
		this.expirationDate = expirationDate;
	}
	public Date getLastUpdateDate() {
		return lastUpdateDate;
	}
	public void setLastUpdateDate(Date lastUpdateDate) {
		this.lastUpdateDate = lastUpdateDate;
	}
	public String getUser() {
		return user;
	}
	public void setUser(String user) {
		this.user = user;
	}
	public ArrayList<String> getServer() {
		return server;
	}
	public void setServer(ArrayList<String> server) {
		this.server = server;
	}
	public String getEmailString() throws NoSuchAlgorithmException, UnsupportedEncodingException
	{
		String str="";
		for(String jString : this.getEmail())
		{
			str+="<a href='/email/"+Base64.encode(jString.getBytes())+"'>"+Utils.returnWhois(jString)+"</a>"+"<br/>";
			
		}
		return str;
	}
	public String getServerString()
	{
		String str="";
		for(String jString : this.getServer())
		{
			str+="<a href='/server/"+jString+"' title='"+jString+"'>"+jString+"</a>"+"<br/>";
			
		}
		return str;
	}
}
